package com.shijunhao.graduation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shijunhao.graduation.annotations.OperationLog;
import com.shijunhao.graduation.domain.bean.ResultCode;
import com.shijunhao.graduation.domain.entity.BusinessUserInfo;
import com.shijunhao.graduation.domain.entity.OrgUser;
import com.shijunhao.graduation.domain.vo.CurrentUserVo;
import com.shijunhao.graduation.properties.OperationTarget;
import com.shijunhao.graduation.properties.OperationType;
import com.shijunhao.graduation.service.business.IBusinessUserInfoService;
import com.shijunhao.graduation.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shijunhao
 * @since 2022-04-08
 */
@RestController
@RequestMapping("/business-user-info")
@Api(tags = "用户信息")
public class BusinessUserInfoController {

    @Resource
    private IBusinessUserInfoService businessUserInfoService;

    @PostMapping("update")
    @ApiOperation("更新")
    @OperationLog(target = OperationTarget.USER,type = OperationType.UPDATE,description = "用户更新信息")
    public ResultCode<Boolean> update(@RequestBody BusinessUserInfo dto) {
        boolean update = businessUserInfoService.update(dto);
        if (update)
            return ResultCode.success("更新成功");
        else
            return ResultCode.error("更新失败");
    }


    @GetMapping("list")
    @ApiOperation("列表")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ResultCode<Page<CurrentUserVo>> page() {
        Page<BusinessUserInfo> page = businessUserInfoService.page(PageUtils.getPage(),new QueryWrapper<>());
        Page<CurrentUserVo> convert = (Page<CurrentUserVo>) page.convert(businessUserInfoService::toVo);
        return ResultCode.success(convert);
    }


    @GetMapping("/get/{id}")
    @ApiOperation("根据id查找")
    public ResultCode<CurrentUserVo> getById(@PathVariable Long id){
        BusinessUserInfo info = businessUserInfoService.getById(id);
        if(info != null){
            return ResultCode.success(businessUserInfoService.toVo(info));
        }else{
            return ResultCode.error("未找到该用户信息");
        }

    }

    @GetMapping("/getByUid/{id}")
    @ApiOperation("根据用户id查找")
    public ResultCode<CurrentUserVo> getByUid(@PathVariable Long id){
        BusinessUserInfo info = businessUserInfoService.getOne(Wrappers.lambdaQuery(BusinessUserInfo.class).eq(BusinessUserInfo::getUid,id));
        if(info != null){
            return ResultCode.success(businessUserInfoService.toVo(info));
        }else{
            return ResultCode.error("未找到该用户信息");
        }

    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @OperationLog(target = OperationTarget.USER,type = OperationType.DELETE,description = "删除用户信息")
    public ResultCode<Boolean> delete(@PathVariable Long id) {
        return ResultCode.success("删除成功",businessUserInfoService.delete(id));
    }

}
